package com.product.controller.admin;

import com.product.pojo.dto.DishDTO;
import com.product.pojo.dto.DishPageQueryDTO;
import com.product.pojo.entity.Dish;
import com.product.pojo.vo.DishVO;
import com.product.service.DishService;

import com.sky.result.PageResult;
import com.sky.result.Result;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
       @Autowired
       DishService dishService;
       @Autowired
       RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @param dish
     * @return
     */
       @PostMapping
       @ApiOperation("新增菜品")
       public Result save(@RequestBody DishDTO dish) {
             //输出日志
             log.info("新增菜品：{}", dish);
             //调用service层
             dishService.saveWithFlavor(dish);
             clearCache("dish_"+dish.getCategoryId());
             return Result.success();
       }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
       @GetMapping("page")
       @ApiOperation("菜品分页查询")
       public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
              log.info("分页查询：{}", dishPageQueryDTO);
              PageResult pageResult=dishService.pageQuery(dishPageQueryDTO);
              return Result.success(pageResult);
       }

     /**
     * 删除菜品的id
     * @param ids
     * @return
     */
       @DeleteMapping
       @ApiOperation("删除菜品")//没有指明前后端参数要统一
       public Result delete(@RequestParam List<Long> ids) {
              log.info("删除菜品的id：{}", ids);
              dishService.deleteBatch(ids);
              clearCache("dish_*");
              return Result.success();
       }

     /**
     * 查询菜品的id
     * @param id
     * @return
     */
       @GetMapping("{id}")
       @ApiOperation("根据id查询菜品的")
       public  Result<DishVO> getById(@PathVariable Long id) {
               log.info("查询菜品的id");
               DishVO dishVO=dishService.getDishWithFlavorById(id);
               return Result.success(dishVO);
       }

     /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
       @PutMapping
       @ApiOperation("修改菜品")
       public  Result update(@RequestBody DishDTO dishDTO) {
               log.info("修改的菜品信息：{}", dishDTO);
               //如果修改的是一个分类就比较麻烦了所以全部清除把
               clearCache("dish_*");
               dishService.updateDishWithFlavor(dishDTO);
               return Result.success();
       }
     /**
     * 起停售设置
     * @param status
     * @param id
     * @return
     */
       @PostMapping("status/{status}")
       @ApiOperation("起停售设置")
       public Result updateStatus(@PathVariable Integer status, @RequestParam Integer id) {
              log.info("当前菜品的状态：{}", status);
              log.info("当前用户id:{}",id);
              clearCache("dish_*");
              dishService.updateStatusById(status,id);
              return Result.success();
       }
      /**
      * 根据分类id查询菜品
      *
      *
      */
      @GetMapping("list")
      @ApiOperation("根据分类菜品id查询菜品")
      public Result<List<Dish>> listById(String categoryId){
             log.info("当前分类id:{}",categoryId);
             List<Dish>list=dishService.listByCategoryId(categoryId);
             return Result.success(list);
      }
      //将清理缓存的方法抽离出来
      private void clearCache(String pattern){
             Set set=redisTemplate.keys(pattern);
             redisTemplate.delete(set);

      }

    /**
     * map统计数量
     * @param map
     * @return
     */
      @GetMapping("countMap")
      @ApiOperation("根据map统计菜品数量")
      public Result<Integer> countByMap(@RequestParam Map<String,Object> map) {
             return dishService.countByMap(map);
      }


}
